package com.imedway.ihe.dtz.xds.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz<br/>
 * @date 2024/7/4
 */
@Slf4j
public class DhccFileUtils {
	
	 public static void saveToFile(InputStream inputStream, File file) throws IOException {
	        if (file.exists()) {
	            file.delete();
	        }
	        file.createNewFile();
	        try (OutputStream outputStream = new FileOutputStream(file)) {
	            byte[] buffer = new byte[1024];
	            int length;
	            while ((length = inputStream.read(buffer)) != -1) {
	                outputStream.write(buffer, 0, length);
	            }
	        }
	    }
	 
	 public static void replaceToFile(InputStream inputStream, File file,File oldfile) throws IOException {
		    if (oldfile.exists()) {
		    	oldfile.delete();
	        }
	        file.createNewFile();
	        try (OutputStream outputStream = new FileOutputStream(file)) {
	            byte[] buffer = new byte[1024];
	            int length;
	            while ((length = inputStream.read(buffer)) != -1) {
	                outputStream.write(buffer, 0, length);
	            }
	        }
	 }
	 
	 public static void appendToFile(InputStream inputStream, File file) throws IOException {
	        if (file.exists()) {	        	
		        try (FileOutputStream outputStream = new FileOutputStream(file,true)) {
		            byte[] buffer = new byte[1024];
		            int length;
		            while ((length = inputStream.read(buffer)) != -1) {
//		            	log.debug(buffer.toString());
		                outputStream.write(buffer, 0, length);
		            }
		        }
	        }else {
	        	log.info("File do not exists");
	        }
	 }
	 
	 public static void transformToFile(InputStream inputStream, File file) throws IOException {
		 saveToFile(inputStream, file);
	 }
	 public static void transformToFile2(InputStream inputStream, File file) throws IOException {
		 String inputFile = "input.txt"; // 输入文件路径
	        String outputFile = "output.txt"; // 输出文件路径
	 
	        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
	             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
	            String line;
	            while ((line = reader.readLine()) != null) {
	                writer.write(line);
	                writer.newLine(); // 写入新行
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	 }
	 
	 public static void createDirectory (String newDirectory) {
		 
		    File dir = new File(newDirectory);		 
	        // 如果文件夹不存在，则创建文件夹
	        if (!dir.exists()) {
	            boolean result = dir.mkdir(); // 创建文件夹
	            if (result) {
	                log.info("Directory created successfully");
	            } else {
	            	log.info("Failed to create directory");
	            }
	        } else {
	        	log.info("Directory already exists");
	        }
	 }
	 
	 public static String getFileType(String type)  {
		 String fileType = null;
//	    	text/plain 表示文本文件的默认值。
//	    	application/octet-stream 表示所有其他情况的默认值。
//	        超文本标记语言文本 .html、.html：text/html
//	        普通文本 .txt： text/plain
//	        RTF 文本 .rtf： application/rtf
//	        GIF 图形 .gif： image/gif
//	        JPEG 图形 .jpeg、.jpg： image/jpeg
//	        au 声音文件 .au： audio/basic
//	        MIDI 音乐文件 mid、.midi： audio/midi、audio/x-midi
//	        RealAudio 音乐文件 .ra、.ram： audio/x-pn-realaudio
//	        MPEG 文件 .mpg、.mpeg： video/mpeg
//	        AVI 文件 .avi： video/x-msvideo
//	        GZIP 文件 .gz： application/x-gzip
//	        TAR 文件 .tar： application/x-tar
		 switch (type) {
		  case "text/plain":
			  fileType="txt";
			  break;
		  case "application/xml":
			  fileType="xml";
			  break;
		  case "text/xml":
			  fileType="xml";
			  break;
		  case "text/html":
			  fileType="html";
			  break;
		  case "application/msword"
		  		+ "":
			  fileType="doc";
			  break;
		  case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
			  fileType="docx";
			  break;
		  case "application/pdf":
			  fileType="pdf";
			  break;
		  case "application/vnd.ms-powerpoint":
			  fileType="ppt";
			  break;
		  case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
			  fileType="pptx";
			  break;
		  case "application/vnd.ms-excel":
			  fileType="xls";
			  break;
		  case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
			  fileType="xlsx";
			  break;
		  case "application/zip":
			  fileType="zip";
			  break;
		  default:
			break;
		 }
		return fileType;
				 
	    }
}
